﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;


namespace NetworkIndexer
{
    public class Utilities
    {
        /// <summary>
        /// Holds an HTML file in object form
        /// </summary>
        public class HtmlCreator
        {
            private StringBuilder _htmlPage;
            /// <summary>
            /// Constructor
            /// </summary>
            public HtmlCreator()
            {
                _htmlPage = new StringBuilder();
            }

            /// <summary>
            /// Initialise the HTML page
            /// </summary>
            public void Init()
            {
                _htmlPage.AppendLine(RenderBeginTag("HTML"));
                _htmlPage.AppendLine(RenderBeginTag("HEAD"));
                _htmlPage.AppendLine(RenderCloseTag("HEAD"));
                _htmlPage.AppendLine(RenderBeginTag("BODY"));

            }

            private string RenderBeginTag(string tag)
            {
                return string.Format("<{0}>", tag.ToString());
            }

            private string RenderCloseTag(string tag)
            {
                return string.Format("</{0}>", tag.ToString());
            }

            /// <summary>
            /// Renders a link and appends to the current html page
            /// </summary>
            /// <param name="link"></param>
            /// <param name="name"></param>
            /// <returns></returns>
            public void AddLink(string link, string name)
            {
                _htmlPage.AppendLine(string.Format("<a href='{0}'>{1}</a></br>", link, name));

            }

            /// <summary>
            /// Finalises the html page
            /// </summary>
            public void Finalise()
            {
                _htmlPage.AppendLine(RenderCloseTag("BODY"));
                _htmlPage.AppendLine(RenderCloseTag("HTML"));
            }

            /// <summary>
            /// Creates the html file
            /// </summary>
            /// <param name="filePath"></param>
            /// <param name="fileName"></param>
            public void CreateFile(string filePath, string fileName)
            {
                File.AppendAllText(Path.Combine(filePath, fileName), _htmlPage.ToString());
            }

        }
    }
}
